Creating a bootable DOS USB stick

A USB stick with a bootable DOS installation is useful for all kinds of things like BIOS updates, or just running old DOS software without having to dedicate a disk partition to it. But creating one completely defeated me, despite several days and nights of effort.

And then I came across http://www.stilen.com/notes/usb_dos_boot.txt (no longer active), which I found so useful, I mirrored it here.

Note that these aren't instructions; they're an annotated script originally designed to run under (I think) Gentoo linux. Some intelligent interpretation is required as you follow this; it's not an exact recipe!

Title:    Bootable FreeDOS USB Flash Drive in Linux
Subject:  To update the bios, I created a usb DOS boot disk in linux.
By:       John Stile &  Alex Belits
--------------------------------------------------------

Note:
  The hard (undocumented) part involved chaning the disk geometry.
  Without that step, I kept seeing the error:
    "bad or missing command interpreter"
  Alex knew how to get around this!

    -------------------------------------------------
    Full error message before setting drive geometry:
    -------------------------------------------------
    FreeDOS kernel verison 2006 cvs [version Aug 18 2006 compiled Aug 18 2006]
    Kernel compatibility 7.10 - WATCOMC - 80386 CPU required - FAT32 support

    Copyright 1995-2006 Pasquale J. Villani and The FreeDOS Project.
    All Rights Reseved. This is free software and comes with ABSOLUTELY NO
    WARRANTY; you can redistribute it and/or modify it under the terms of the
    GNU General Public License as published by the Free Software Foundation;
    either version 2, or (at your option) any later version.
     - InitDiskWARNING: using suspect partition Pri:1 FS 0b: with calculated values
     0-0-63 insted of 0-1-1
    WARNING: using suspect partition Pri:1 FS 0b: with calculated values 976-8-8 instead of 1023-30-62
    C: HD1, Pri[ 1], CHS=    0-0-63, start=    0 MB, size=960 MB
    Bad or missing Command Interpreter: command.com /P /E:256
    Enter the full shell command line:

###################################################################################
# Procedure:
#    Install FreeDOS, syslinux, mkbootfat
#    Shred usb disk info,
#    Run makebootfat to install MBR on usb,
#    Change disk geomertry,
#    Recreate partition,
#    Run makebootfat on partition,
#    Boot to FreeDOS from usb
###################################################################################

#
# My usb disk
#
export DISK=/dev/sdb

#----------------------------
# FreeDOS - Download & Mount
#
#   Provides:
#    command.com
#    kernel.sys
#   AND
#    fat12.bin
#    fat16.bin
#    fat32lba.bin
#
#----------------------------

wget http://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/distributions/1.0/fdfullcd.iso

mkdir Freedos_fdfullcd
mount -o loop fdfullcd.iso Freedos_fdfullcd

mkdir boot_root
cp ./Freedos_fdfullcd/freedos/setup/odin/{command.com,kernel.sys} boot_root/
# Optionally, copy  in all *.com and *.sys files.

mkdir boot_kernels
unzip ./Freedos_fdfullcd/freedos/packages/src_base/kernels.zip  -d boot_kernels

#-----------------------------
# syslinux - Install
#
#   Provides:
#     mbr.bin
#
#-----------------------------

emerge --deep -ta syslinux
equery files syslinux |grep mbr.bin
#    /usr/lib/syslinux/mbr.bin

#-----------------------------
# makebootfat - Install
#
#   Provides:
#     makebootfat
#
#-----------------------------

wget http://prdownloads.sourceforge.net/advancemame/makebootfat-1.4.tar.gz?download
tar -xvf makebootfat-1.4.tar.gz
cd makebootfat-1.4
./configure
make
make install
export PATH=$PATH:/usr/local/bin

#-----------------------------
# coreutils - Install
#
#   Provides:
#     shred
#
#-----------------------------

emerge sys-apps/coreutils

#-----------------------------
# Remove partitions
#-----------------------------

shred -n 1 --size=1k $DISK
sync

#-----------------------------
# Show current geometry
#-----------------------------

fdisk -l $DISK

     Disk /dev/sdb: 1007 MB, 1007681536 bytes
     31 heads, 62 sectors/track, 1024 cylinders
     Units = cylinders of 1922 * 512 = 984064 bytes
     Disk identifier: 0xa1abf5ad

     Disk /dev/sdb doesn't contain a valid partition table

#-----------------------------
# Add MBR on to Disk
#-----------------------------

makebootfat \
-o $DISK    \
-E 255      \
-1 ./boot_kernels/source/ukernel/boot/fat12.bin    \
-2 ./boot_kernels/source/ukernel/boot/fat16.bin    \
-3 ./boot_kernels/source/ukernel/boot/fat32lba.bin \
-m /usr/lib/syslinux/mbr.bin \
-D   boot_root

#-----------------------------
# FIX geometry,
#  Sectors & Heads changes
#  based on error message when
#  command.com couldn't be
#  found.
# AND
#  setup a parition on the usb
#-----------------------------

fdisk -S 63 -H 32  $DISK

    Command (m for help): d
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    Partition number (1-4): 1
    First cylinder (1-976, default 1):
    Last cylinder or +size or +sizeM or +sizeK (1-976, default 976):
    Command (m for help): t
    Hex code (type L to list codes): c
    Command (m for help): a
    Command (m for help): p
    Command (m for help): w

#-----------------------------
# Look at Disk Now
#-----------------------------

fdisk -l $DISK

    Disk /dev/sdb: 1007 MB, 1007681536 bytes
    32 heads, 63 sectors/track, 976 cylinders
    Units = cylinders of 2016 * 512 = 1032192 bytes
    Disk identifier: 0x000f35d4

       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1   *           1         976      983776+   c  W95 FAT32 (LBA)

#-----------------------------
# Unmount the disk if  mounted
#-----------------------------

umount ${DISK}1

#-----------------------------
# Install image into Partition
#-----------------------------

makebootfat \
-o ${DISK}1 \
-1 ./source/ukernel/boot/fat12.bin \
-2 ./source/ukernel/boot/fat16.bin \
-3 ./source/ukernel/boot/fat32lba.bin  \
-P   boot_root

#-----------------------------
# Boot to FreeDOS with usb
#-----------------------------

Yay!